﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace AccumulateApp.View
{
    /// <summary>
    /// 图片颜色拾取器窗口
    /// </summary>
    public partial class ImageColorPickerWindow : Window
    {
        public ImageColorPickerWindow()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 拾色器，鼠标移动时获取当前像素的颜色，显示在文本框中
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void imgLogo_MouseMove(object sender, System.Windows.Input.MouseEventArgs e)
        {
            System.Windows.Point position = e.GetPosition(imgLogo);
            BitmapSource bitmapSource = (BitmapSource)imgLogo.Source;

            if (bitmapSource != null)
            {
                int x = (int)(position.X * bitmapSource.PixelWidth / imgLogo.ActualWidth);
                int y = (int)(position.Y * bitmapSource.PixelHeight / imgLogo.ActualHeight);

                if (x >= 0 && x < bitmapSource.PixelWidth && y >= 0 && y < bitmapSource.PixelHeight)
                {
                    CroppedBitmap cb = new CroppedBitmap(bitmapSource, new Int32Rect(x, y, 1, 1));
                    byte[] pixels = new byte[4];
                    cb.CopyPixels(pixels, 4, 0);

                    TextA.Text = pixels[3].ToString();
                    TextR.Text = pixels[2].ToString();
                    TextG.Text = pixels[1].ToString();
                    TextB.Text = pixels[0].ToString();
                    var color = System.Windows.Media.Color.FromArgb(pixels[3], pixels[2], pixels[1], pixels[0]);

                }
            }
        }
    }
}
